﻿using System;
using System.Collections.Generic;
using Development.Tools;

namespace Development.Network.MsgHandle
{
    /// <summary>
    /// 消息管理类 可以将所有的handle放在这里统一处理
    /// </summary>
    public class MsgHandleManager : Singleton<MsgHandleManager>
    {
        /// <summary>
        /// handle 列表
        /// </summary>
        private List<MsgHandle> _handles = new List<MsgHandle>();
        /// <summary>
        /// 增加一个消息handle
        /// </summary>
        /// <param name="handle"></param>
        public void AddMsgHandle(MsgHandle handle)
        {
            _handles.Add(handle);
        }

        /// <summary>
        /// 收到消息
        /// </summary>
        /// <param name="msg"></param>
        public void RecvMsg(Msg.MsgPacket msg)
        {
            bool isProcess = false;
            foreach(var h in _handles)
            {
                if(h.IsSelfId(msg.MsgId))
                {
                    h.AddMsg(msg);
                    isProcess = true;
                    break;
                }
            }
            if(!isProcess)
            {
                Log.WriteLog(ELogLevel.LOG_UNKNOWN, "未知消息id = " + msg.MsgId);
            }
        }

        public void Close()
        {
            foreach(var h in _handles)
            {
                h.Release();
            }
            _handles.Clear();
        }
    }
}
